package main

import (
	"demo1/structDemo"
	"fmt"
	"io"
	"log"
	"os"
	"reflect"
	"strings"
	"unsafe"
)

const (
	NAME = "小绿"
)

//func init() {
//	fmt.Println("main init...")
//}

func main() {
	//ans := ms.FindAnagrams("abab", "ab")
	//fmt.Println(ans)
	//arr := [5]int{}
	//arr[0] = 1
	//arr[1] = 2
	//arr[2] = 3
	//arr[3] = 4
	//arr[4] = 5
	//fmt.Println(arr)
	//fmt.Println(arr[:3])
	//isflag := ms.IsAnagram("abcdf", "badcft")
	//fmt.Println(isflag)
	//mall := ms.RansomMall("abc", "aplodnb")
	//fmt.Println(mall)
	//anagrams := ms.FindAnagrams("abab", "ab")
	//fmt.Println(anagrams)
	//strings := make([]string, 10)
	//mapp := make(map[string]string)
	//chan1 := make(chan string)
	//go func() {
	//	defer fmt.Println("goroutine执行结束===")
	//	fmt.Println("goroutine正在执行===")
	//	chan1 <- "666"
	//}()
	//
	//fmt.Println("主线程执行")
	//msg := <-chan1
	//fmt.Println("msg===", msg)
	//open, err := os.Open("D:\\iotest\\readtxt\\111.txt")
	//if err != nil {
	//	log.Fatal(err)
	//}
	//defer open.Close()
	//bytes, err := io.ReadAll(open)
	//if err != nil {
	//	log.Fatal(err)
	//}
	//split := strings.Split(string(bytes), "\n")
	//for _, s := range split {
	//	fmt.Println(s)
	//}
	//readFile()
	//测试panic
	//defer func() {
	//	if r := recover(); r != nil {
	//		fmt.Println("panic occurred====")
	//	}
	//}()
	//panic("触发panic函数====")
	//fmt.Println("执行业务逻辑====")
	//f1("1", 2, 3, 4)
	//b := getBook("1q84", "奇妙经历")
	//fmt.Println(b.name)
	//arr1 := []int{1, 2, 3, 4}
	//fmt.Println(arr1)
	//f2(arr1)
	//fmt.Println(arr1)
	//fmt.Println("arr2更改前===", arr1)
	//arr2 := arr1
	//arr2[2] = 99
	//fmt.Println("arr2===", arr2)
	//fmt.Println("arr2更改后arr1===", arr1)
	//for _, t := range arr1 {
	//	t = 2
	//	fmt.Println(t)
	//}
	//fmt.Println(arr1)
	//for i := 0; i < len(arr1); i++ {
	//	arr1[i] = 2
	//}
	//fmt.Println(arr1)
	//b := book{name: "小兰", desc: "aaa"}
	//fmt.Println(b)
	//f3(&b)
	//fmt.Println(b)
	//arr := make([]int, 4)
	//arr[0] = 0
	//arr[1] = 1
	//arr[2] = 2
	//arr[3] = 3
	//fmt.Println(arr)
	//f2(arr)
	//fmt.Println(arr)
	//ma := make(map[string]string)
	//ma["a"] = "aa"
	//ma["b"] = "bb"
	//ma["c"] = "cc"
	//fmt.Println(ma)
	//f4(ma)
	//fmt.Println(ma)
	//arr := [3]int{1, 2, 3}
	//fmt.Println(arr)
	//arr1 := arr
	//arr1[0] = 99
	//fmt.Println(arr1)
	//fmt.Println(arr)
	//arr := make([]int, 5)
	//arr[0] = 0
	//arr[1] = 1
	//arr[2] = 2
	//arr[3] = 3
	//arr[4] = 4
	//arr1 := arr[1:4]
	//fmt.Println(arr1)
	//fmt.Println(len(arr1))
	//fmt.Println(cap(arr1))

	//mk := make(map[int]string)
	//mk[0] = "aa"
	//mk[1] = "bb"
	//mk[2] = "cc"
	////fmt.Println(mk)
	//s, ok := mk[2]
	//fmt.Println("ok=======;", ok)
	//fmt.Println(s)
	//var a = 0
	//fmt.Println(&a)
	//fmt.Println("无聊。。")
	//var i1 = 10
	//fmt.Println("修改前i1==", i1)
	//f10(&i1)
	//fmt.Println("修改后i1==", i1)
	//fmt.Println(*i1)
	//for i := 0; i < 10; i++ {
	//	fmt.Println(11)
	//}
	//arr1 := [4]int{10, 11, 12, 13}
	//fmt.Println(arr1)
	//for i, _ := range arr1 {
	//	fmt.Println("索引值==", i)
	//	arr1[i] = 2
	//}
	//fmt.Println(arr1)
	//var a1 = "123"
	//var a2 = "123"
	//fmt.Println(a1 == a2)
	//myArray := [4]int{1, 2, 3, 4}
	//fmt.Println(myArray)
	//user1 := structDemo.User{Name: "小蓝1", Age: 22}
	//user2 := structDemo.User{Name: "小蓝2", Age: 23}
	//user3 := structDemo.User{Name: "小蓝3", Age: 24}
	//arr := [3]structDemo.User{user1, user2, user3}
	//for _, i := range arr {
	//	fmt.Println(i.Age)
	//}
	//fmt.Println("==========")
	//f12(&arr)
	//fmt.Println("=========")
	//fmt.Println("f12方法执行完毕后===", arr)
	//f13("aa", 3, 4, 5)
	//f14()
	//f15()
	//f16()
	//f17()
	//f18()
	a := 3
	fmt.Println("原a值为==", a)
	fmt.Println(unsafe.Pointer(&a))
	fmt.Println("===========")
	f19(&a)
	fmt.Println("===========")
	fmt.Println(unsafe.Pointer(&a))
	fmt.Println("新a值为==", a)
}

func f19(pp *int) {
	//pp与&a指向同一个内存地址(即变量a的地址)，但pp是一个新的变量，有自己独立的内存空间
	//fmt.Println(unsafe.Pointer(&pp))打印的是pp这个指针变量本身的内存地址，而不是指向变量a的内存地址
	//fmt.Println(unsafe.Pointer(&pp))打印的是变量a的内存地址
	*pp = 99
	fmt.Println(unsafe.Pointer(pp))
	fmt.Println(*pp) //99
}

func f18() {
	mapp := make(map[string]int)
	//fmt.Println(mapp)
	mapp["a"] = 1
	mapp["b"] = 2
	//fmt.Println(mapp["a"])
	for key, value := range mapp {
		fmt.Println(key)
		fmt.Println(value)
	}
	s, ok := mapp["a"]
	fmt.Println(s)  //value值
	fmt.Println(ok) //true/false
}

func f17() {
	arr := make([]int, 3, 5)
	arr[0] = 1
	arr[1] = 2
	arr[2] = 3
	fmt.Println(arr)
	fmt.Println("arr地址为", unsafe.Pointer(&arr))
	slices2 := make([]int, 3, 5)
	copy(slices2, arr)
	fmt.Println(slices2)
	fmt.Println("slices2地址为", unsafe.Pointer(&slices2))
}

func f16() {
	arr := make([]int, 3, 5)
	arr[0] = 1
	arr[1] = 2
	arr[2] = 3
	//arr[3] = 4
	//arr[4] = 5
	arr = append(arr, 4)
	arr = append(arr, 8)
	//arr = append(arr, 9)
	fmt.Println("arr为===", arr)
	fmt.Println("arr长度为==", len(arr))
	fmt.Println("arr容量为==", cap(arr))
	address := unsafe.Pointer(&arr[0])
	fmt.Println("变更前arr底层数组地址===", address)
	fmt.Println("变更前===================")
	arr1 := arr
	arr1 = append(arr1, 999)
	arr1[0] = 99
	fmt.Println("变更后==================")
	fmt.Println("arr1为===", arr1)
	fmt.Println("arr1长度为==", len(arr1))
	fmt.Println("arr1容量为==", cap(arr1))
	address1 := unsafe.Pointer(&arr1[0])
	fmt.Println("arr1底层数组地址===", address1)
	fmt.Println("================")
	fmt.Println("arr为===", arr)
	fmt.Println("arr长度为==", len(arr))
	fmt.Println("arr容量为==", cap(arr))
	address2 := unsafe.Pointer(&arr[0])
	fmt.Println("变更后arr底层数组地址===", address2)
}

func f15() int {
	fmt.Println("return 方法执行")
	return 0
}
func f14() int {
	fmt.Println("即将执行defer函数。。。")
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("捕获到panic")
		}
	}()
	panic("收到制造panic。。。")
	return 0
}

func f13(a string, b ...int) {
	fmt.Println("a的值为====", a)
	fmt.Println("b的值为===", b)
}

func f12(arr *[3]structDemo.User) {
	//user1 := structDemo.User{Name: "小蓝1", Age: 22}
	//user2 := structDemo.User{Name: "小蓝2", Age: 23}
	//user3 := structDemo.User{Name: "小蓝3", Age: 24}
	//arr := [3]structDemo.User{user1, user2, user3}
	arr[0].Age = 12
	for _, i := range arr {
		fmt.Println("f12方法内部===", i.Age)
	}
}
func f11() {
	arr := [3]string{"a", "b", "c"}
	//for _, i2 := range arr {
	//	//fmt.Println("i的值为===", i)
	//	fmt.Println("i2的值为===", i2)
	//}
	for i := 0; i < len(arr); i++ {
		fmt.Println(arr[i])
	}
}

func f10(i1 *int) {
	*i1 = *i1 + 1
	fmt.Println(*i1)
}

func f4(m map[string]string) {
	m["b"] = "ppplpl"
}
func f3(b *book) {
	b.name = "kppk"
}

func f2(arr []int) {
	arr[2] = 99
}

type book struct {
	name string
	desc string
}

func getBook(name, desc string) *book {
	return &book{
		name: name,
		desc: desc,
	}
}

func reflectDemo(obj interface{}) {
	inputType := reflect.TypeOf(obj)
	inputValue := reflect.ValueOf(obj)
	fmt.Println("type:==", inputType)
	fmt.Println("value:==", inputValue)
	for i := 0; i < inputType.NumField(); i++ {
		fmt.Println("类型====", inputType.Field(i))
		fmt.Println("值为====", inputValue.Field(i).Interface())
	}

}

func readFile() {
	sourcePath := "D:\\iotest\\readtxt\\111.txt"
	open, err := os.Open(sourcePath)
	if err != nil {
		log.Fatal(err)
	}
	bytes, err := io.ReadAll(open)
	if err != nil {
		log.Fatal(err)
	}
	split := strings.Split(string(bytes), "\n")
	for _, s := range split {
		fmt.Println(s)
	}
}

func f1(a string, b ...int) {
	fmt.Println("=====" + a)
	fmt.Println("b====", b)
}
